package fragment;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.example.administrator.animation_demo.R;

/**
 * Created by eyesee on 2017/5/8.
 */

public class ScaleFragment extends Fragment implements View.OnClickListener {

    private View mView;
    private ImageView mIv;
    private Button mBtStart;
    private Button mBtStop;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mView = View.inflate(getActivity(), R.layout.fragment_scale, null);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initView(mView);
        return mView;
    }

    private void initView(View view) {
        mIv = ((ImageView) view.findViewById(R.id.iv));
        mBtStart = ((Button) view.findViewById(R.id.sc_start));
        mBtStop = ((Button) view.findViewById(R.id.sc_over));

        mBtStart.setOnClickListener(this);
        mBtStop.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.sc_start:
                StartScale(mIv);
                break;
            case R.id.sc_over:
                StopScale(mIv);
                break;
        }
    }

    public void StartScale(View view){
        ObjectAnimator animator = ObjectAnimator.ofFloat(view,"scaleX",1,0.5f,0,1);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(view,"scaleY",1,0.5f,0,1);
        AnimatorSet set = new AnimatorSet();
        set.play(animator).with(animator2);
        set.setDuration(2000);
        set.start();
    }

    public void StopScale(View view){
        ObjectAnimator animator = ObjectAnimator.ofFloat(view,"scaleX",1,0.2f);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(view,"scaleY",1,0.2f);
        AnimatorSet set = new AnimatorSet();
        set.play(animator).with(animator2);
        set.setDuration(2000);
        set.start();
    }

    /**
     * setScaleX(float scaleX):在X轴上缩放，scaleX表示缩放倍数
     * setScaleY(float scaleY):在Y轴上缩放，scaleY表示缩放倍数
     * */
}
